package com.loyin.tool;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 分析url
 * @author 刘声凤
 * 2011-11-11
 */
public class AnalysisUrl {
	private Logger log=Logger.getLogger(this.getClass());
	public int mincount=3;
	/**
	 * 分析url
	 * @param url
	 * @param keyword
	 * @throws IOException 
	 * @throws MalformedURLException 
	 */
	public List<Map<String,Object>> analysis(String url,String keyword) throws Exception{
		String baseurl="";
		if(url.lastIndexOf("/")>6){
			baseurl=url.substring(0,url.lastIndexOf("/"));
		}else{
			baseurl=url+"/";
		}
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		String[] keywords=keyword.split(" ");
		Document doc = Jsoup.parse(new URL(url), 5000);
		Elements alinklist = doc.getElementsByTag("a");
		for(Element a:alinklist){
			String title=a.text();
			int count=0;
			for(String kw:keywords){
				if(title.contains(kw)){
					count++;
				}
			}
			if(count>=mincount){
				String href=a.attr("href");
				Map<String,Object> map=new HashMap<String,Object>();
				if(!href.toLowerCase().startsWith("http://")){
					if(href.toLowerCase().startsWith("../")){
						String urltemp=baseurl;
						if(baseurl.endsWith("/")){
							urltemp=baseurl.substring(0,baseurl.lastIndexOf("/")-1);
						}else{
							urltemp=baseurl.substring(0,baseurl.lastIndexOf("/"));
						}
						href=urltemp+"/"+href.replace("../","");
					}else{
						href=baseurl+"/"+href;
					}
				}
				map.put("title",title);
				map.put("href",href);
				map.put("count",count);
				list.add(map);
			}
		}
		return list;
	}
	public static void main(String[] args){
		try{
			List<Map<String,Object>> list=new AnalysisUrl().analysis("http://www.bjcoc.gov.cn/tzgg3/", "年度 立项 批 名单 项目 通知 企业 科技");
			for(Map<String,Object> map:list){
				System.out.println(map.get("title")+"\t"+map.get("count")+"\t"+map.get("href"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
